package com.tuanzi.loan;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;

import com.tuanzi.loan.security.*;

/**
 * @author Ray 2017/10/01
 */
@Configuration
@EnableWebSecurity
public class SecurityInitializer extends WebSecurityConfigurerAdapter {

	@Value("${spring.security.debug}")
	private Boolean debug;
	@Autowired
	private LoanUserDetailServiceImpl loanUserDetailService;
	@Autowired
	private LoanFilterSecurityInterceptor loanFilterSecurityInterceptor;
	@Autowired
	private LoanAuthenticationSuccessHandler loanAuthenticationSuccessHandler;
	@Autowired
	private LoanAuthenticationFailureHandler loanAuthenticationFailureHandler;
	@Autowired
	private LoanLogoutSuccessHandler loanLogoutSuccessHandler;
	@Autowired
	private LoanInvalidSessionStagegy loanInvalidSessionStagegy;

	@Override
	protected void configure(AuthenticationManagerBuilder auth) throws Exception {
		auth.userDetailsService(loanUserDetailService).passwordEncoder(passwordEncoder());
	}

	@Bean
	public BCryptPasswordEncoder passwordEncoder() {
		return new BCryptPasswordEncoder();
	}

	@Override
	public void configure(WebSecurity web) throws Exception {
		web.ignoring().antMatchers("/javax.faces.resource/**", "/login.xhtml", "/api/**");
		web.expressionHandler(new LoanWebSecurityExpressionHandler());
		web.debug(debug);
	}

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http.csrf().disable();
		http.sessionManagement().invalidSessionStrategy(loanInvalidSessionStagegy);
		http.sessionManagement().maximumSessions(1).expiredUrl(LoanInvalidSessionStagegy.EXPIRED_URL);
		http.sessionManagement().sessionFixation().migrateSession();
		http.addFilterBefore(loanFilterSecurityInterceptor, FilterSecurityInterceptor.class);
		http.exceptionHandling().accessDeniedPage("/403.xhtml");
		http.formLogin().usernameParameter("username").passwordParameter("password").loginPage("/login.xhtml").loginProcessingUrl("/login-process")
				.successHandler(loanAuthenticationSuccessHandler).failureHandler(loanAuthenticationFailureHandler);
		http.logout().deleteCookies("JSESSIONID").logoutUrl("/logout-process").clearAuthentication(true).invalidateHttpSession(true).logoutSuccessHandler(loanLogoutSuccessHandler);
	}
}
